package com.jic.market.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.market.common.enums.DeleteFlagEnum;
import com.jic.market.common.util.BeanUtil;
import com.jic.market.entity.PatchGrouponThemeEntity;
import com.jic.market.entity.PromotionEntity;
import com.jic.market.entity.PromotionMutex;
import com.jic.market.enums.PromotionTypeEnum;
import com.jic.market.mapper.PatchGrouponThemeEntityMapper;
import com.jic.market.mapper.PromotionEntityMapper;
import com.jic.market.mapper.PromotionMerchantProductEntityMapper;
import com.jic.market.mapper.PromotionMutexMapper;
import com.jic.market.request.PromotionMutexRequest;
import com.jic.market.response.PromotionMutexDetailResponse;
import com.jic.market.service.PromotionMutexService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 促销商品互斥表
 * @author : MJ
 * @email :
 * @since : 2020-05-11
 * @version : v1.0.0
 */
@Service
public class PromotionMutexServiceImpl implements PromotionMutexService{

    @Autowired
    private PromotionMutexMapper promotionMutexMapper;

    @Autowired
    private PromotionEntityMapper promotionEntityMapper;

    @Autowired
    private PatchGrouponThemeEntityMapper patchGrouponThemeEntityMapper;

    @Resource
    private PromotionMerchantProductEntityMapper promotionMerchantProductEntityMapper;

    @Resource
    private SysDeptApi sysDeptService;

    @Override
    public long insert(PromotionMutex promotionMutex) {
        long flag = 0;
        PromotionMutex param = new PromotionMutex();
        param.setPromotionMerchantProductId(promotionMutex.getPromotionMerchantProductId());
        param.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        param.setPromotionId(promotionMutex.getPromotionId());
        param.setPromType(promotionMutex.getPromType());
        if(promotionMutexMapper.queryPageCount(param) <= 0){
           promotionMutexMapper.insert(promotionMutex);
        }
        return flag;
    }

    @Override
    public long delete(Long id) {
        return promotionMutexMapper.delete(id);
    }

    @Override
    public long update(PromotionMutex promotionMutex) {
        return promotionMutexMapper.update(promotionMutex);
    }

    @Override
    public PromotionMutex queryByPrimaryKey(Long id){
        return promotionMutexMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<PromotionMutex> query(PromotionMutex promotionMutex){
        return promotionMutexMapper.query(promotionMutex);
    }

    @Override
    public PageResult<PromotionMutex> queryPage(PromotionMutex promotionMutex, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<PromotionMutex> list = promotionMutexMapper.queryPage(startIndex, pageQuery.getLimit(), promotionMutex);
        long count = promotionMutexMapper.queryPageCount(promotionMutex);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * 根据互斥商品查询对应互斥活动
     * @param request
     * @return
     */
    @Override
    public PageResult<PromotionMutexDetailResponse> getPromotionMutexListByProductId(PromotionMutexRequest request) {
        PageResult<PromotionMutexDetailResponse> pageResult = new PageResult<>();
        int startIndex = (request.getStart() - 1) * request.getLimit();
        //获取所有商品互斥
        List<PromotionMutexDetailResponse> list  = promotionMutexMapper.queryAllByProductId(startIndex, request.getLimit(),request.getPromotionMerchantProductId());
        long total = promotionMutexMapper.queryAllByProductIdCount(startIndex, request.getLimit(),request.getPromotionMerchantProductId());

        List<PromotionMutexDetailResponse> responseList = new ArrayList();
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(promotionMutex -> {
                if(PromotionTypeEnum.GROUPON.getCode().equals(promotionMutex.getPromType())){
                    PatchGrouponThemeEntity patchGrouponThemeEntity = patchGrouponThemeEntityMapper.queryById(promotionMutex.getPromotionId());
                    if(null!=patchGrouponThemeEntity) {
                        PromotionMutexDetailResponse promotionMutexResponse = BeanUtil.copy(patchGrouponThemeEntity, PromotionMutexDetailResponse.class);
                        RestResult<String> dept = sysDeptService.queryDeptNameById(patchGrouponThemeEntity.getDeptId());
                        promotionMutexResponse.setPromotionId(promotionMutex.getPromotionId());
                        promotionMutexResponse.setPromType(promotionMutex.getPromType());
                        promotionMutexResponse.setDeptName(dept.getData());
                        promotionMutexResponse.setPromName(patchGrouponThemeEntity.getPromName());
                        promotionMutexResponse.setPromotionCode(patchGrouponThemeEntity.getGroupCode());
                        promotionMutexResponse.setStatus(patchGrouponThemeEntity.getStatus());
                        responseList.add(promotionMutexResponse);
                    }
                }else{
                    PromotionEntity promotionEntity = promotionEntityMapper.queryById(promotionMutex.getPromotionId());
                    if(null!=promotionEntity){
                        PromotionMutexDetailResponse promotionMutexResponse = BeanUtil.copy(promotionEntity,PromotionMutexDetailResponse.class);
                        Long deptId = promotionEntity.getDeptId();
                        RestResult<String> dept =  sysDeptService.queryDeptNameById(deptId);
                        promotionMutexResponse.setPromotionId(promotionMutex.getPromotionId());
                        promotionMutexResponse.setPromType(promotionMutex.getPromType());
                        promotionMutexResponse.setDeptName(dept.getData());
                        promotionMutexResponse.setStatus(promotionEntity.getStatus());
                        responseList.add(promotionMutexResponse);
                    }
                }
            });
        }
        pageResult.setRows(responseList);
        pageResult.setTotal(total);
        return pageResult;
    }

    @Override
    public int deleteByPromotionProductId(Long promotionProductId) {
        return promotionMutexMapper.deleteByPromotionProductId(promotionProductId);
    }

    @Override
    public int deleteByPromotionIdAndPromType(Long promotionId, Integer promType) {
        return promotionMutexMapper.deleteByPromotionIdAndPromType(promotionId, promType);
    }

    @Override
    public int insertBatch(List<PromotionMutex> collect) {
        return promotionMutexMapper.insertBatch(collect);
    }
}